<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 * 余额充值处理
 */
 
namespace osc\payment\controller;
use osc\common\controller\Base;
use payment\weixin\WxPayApi;
use payment\weixin\WxPayConfig;
use payment\weixin\WxPayUnifiedOrder;
use payment\weixin\WxPayNotifyCallBack;
use think\Db;
class Recharge extends Base{
	//支付宝异步通知
	function alipay_notify(){
		
		$alipay= new \payment\alipay\Alipay(payment_config('alipay'));	
		
		$verify_result = $alipay->verifyNotify();
		
		if($verify_result) {		
			
			$post=input('post.');
			
			$order=Db::name('predeposit')->where('order_num_alias',$post['out_trade_no'])->find();
			
			if($post['trade_status'] == 'TRADE_FINISHED') {				
				
		    }elseif($post['trade_status'] == 'TRADE_SUCCESS') {		
				
				if($order&&($order['pay_status']!=1)){
					
					$api=osc_service('common','Predeposit');
										
					$api->update_recharge_order($order['p_id'],$order['uid'],$order['money']);
					
					echo "success";		
									
				}else{
					echo "fail";
				}		        
				
		    }			
			
		}else{
			
			echo "fail";
		}
	}
	//支付宝同步通知
	function alipay_return(){
		
		$alipay= new \payment\alipay\Alipay(payment_config('alipay'));		
		//对进入的参数进行远程数据判断
		$verify = $alipay->return_verify();
	
		if($verify){
		
			$get=input('param.');
			
			$order=Db::name('predeposit')->where('order_num_alias',$get['out_trade_no'])->find();
			
			if($order['pay_status']==1){
				@header("Location: ".url('/pay_success'));	
				die;
			}
			
			if($order&&($order['pay_status']!=1)){
				//支付完成
				if($get['trade_status']=='TRADE_SUCCESS'){
										
					$api=osc_service('common','Predeposit');
					
					$api->update_recharge_order($order['p_id'],$order['uid'],$order['money']);
					
					@header("Location: ".url('/pay_success'));	
				}						
			}else{
				die('订单不存在');
			}
			
		}else{
			die('支付失败');
		}
	}
	
	//微信支付二维码
	function weixin_code(){
		
		$id=(int)input('param.id');
		
		$order=Db::name('predeposit')->where('p_id',$id)->find();
		
		if($order&&($order['pay_status']!=1)){
		
			$config=payment_config('weixin');
	
			$cfg = array(
			    'APPID'     => $config['appid'],
			    'MCHID'     => $config['weixin_partner'],
			    'KEY'       => $config['partnerkey'],
			    'APPSECRET' => $config['appsecret'],
			    'NOTIFY_URL' =>request()->domain().url('Payment/Recharge/weixin_notify')
		    );
		
			
		    WxPayConfig::setConfig($cfg);     
	        //②、统一下单
	        $input = new WxPayUnifiedOrder();           
	  
	        $input->SetBody('余额充值');
	        $input->SetAttach('附加数据');
	        $input->SetOut_trade_no($order['order_num_alias']);
			
	        $input->SetTotal_fee((float)$order['money']*100);
			
	        $input->SetTime_start(date("YmdHis"));
	        $input->SetTime_expire(date("YmdHis", time() + 600));
			$input->SetTrade_type('NATIVE');
	
			$input->SetProduct_id(time());
			
			$wxapi=new WxPayApi();
			
		    $url= $wxapi->unifiedOrder($input);				
			
			$this->assign('url',$url['code_url']);
			
			$this->assign('trade_no',$order['order_num_alias']);
						
			return $this->fetch('code'); 
		}
	}
	
	public function weixin_notify(){	
		
		$config=payment_config('weixin');
		
		$notify_url=request()->domain().url('payment/recharge/weixin_notify');
	
		$cfg = array(
			'APPID'     => $config['appid'],
			'MCHID'     => $config['weixin_partner'],
			'KEY'       => $config['partnerkey'],
			'APPSECRET' => $config['appsecret'],
			'NOTIFY_URL' => $notify_url,
		);
		WxPayConfig::setConfig($cfg); 	
	
		$call_back=new WxPayNotifyCallBack();
		
		$data=$call_back->Handle(false);
		
		if($data&&$data['result_code']=='SUCCESS'){
						
			$order=Db::name('predeposit')->where('order_num_alias',$data['out_trade_no'])->find();		
			
			if($order&&($order['pay_status']!=1)){				
					$api=osc_service('common','Predeposit');
					
					$api->update_recharge_order($order['p_id'],$order['uid'],$order['money']);				
			}
			
            echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        }else{
        	
            echo '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
        }
		
	}

	public function get_order_status(){
		
		$data=input('param.');
		
		$order=Db::name('predeposit')->where('order_num_alias',$data['out_trade_no'])->find();	
		
		if($order['pay_status']==1){
			die('pay_suc');
		}else{
			die('no_pay');
		}
	}
}
